# Copyright (c) 2007 Fabio Makoto Akita
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Author: AkitaOnRails (www.akitaonrails.com)
# Support: Surgeworks LLC (www.surgeworks.com)
class SyncsController < ApplicationController
  include SyncsHelper::ServerActions
  include SyncsHelper::Handshake
  include SyncsHelper::Request
  include SyncsHelper::NetSupport
  include SyncsHelper::Misc
  include SyncsHelper::Processes

  # skip 3rd party authentication in this controller
  # skip_before_filter :login_from_cookie
  # skip_before_filter :login_required
  before_filter :authenticated?, :only => [:perform_sync, :up, :down]
  
  def perform_sync
    if request.xhr?
      unless job_running?
        start_runner :replicator
        render(:update) do |page|
          page['sync_button'].disabled = true # this will flag the dump_sync_log observer
          page['busy'].toggle
        end
      else
        render(:update) do |page|
          page.alert('Data Replication already running.')
        end
      end
    end
  end
  
  def dump_sync_log
    if request.xhr?
      @buffer = read_log(SyncsHelper::ReplicatorRunner.log_file)
      @has_finished = !job_running?
      
      render(:update) do |page|
        page.replace_html('dump_log', @buffer) unless @buffer.empty?
        if @has_finished
          page['sync_button'].disabled = false
          page['busy'].toggle
          page.alert('Data Replication Finished.')
        end
      end
    end
  end
  
  def perform_upgrade
    if request.xhr?
      unless job_running?
        start_runner :upgrade
        render(:update) do |page|
          page['sync_button'].disabled = true # this will flag the dump_sync_log observer
          page['busy'].toggle
        end
      else
        render(:update) do |page|
          page.alert('Software Upgrade already running.')
        end        
      end
    end
  end
  
  def dump_upgrade_log
    if request.xhr?
      @buffer = read_log(SyncsHelper::UpgradeRunner.log_file)
      @has_finished = !job_running?
      
      render(:update) do |page|
        page.replace_html('dump_log', @buffer) unless @buffer.empty?
        if @has_finished
          page['sync_button'].disabled = false
          page['busy'].toggle
          page.alert('Software Upgrade Finished. Restarting Application Recommended')
        end
      end
    end
  end
  
  # to be overridden
  def down_extra_conditions
    # return finder conditions array like ["column = ?", value]
  end
  
  # to be overridden
  def dump_conditions(model_class)
    # return finder conditions array like ["column = ?", value]
  end
end